package com.hls.core.aspect;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hls.core.utils.JSONChange;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

/**
 * @author weigu
 */
@Component
@Aspect
@Slf4j
public class LogAspect {

    public LogAspect(){}

    @Pointcut(value = "execution(public * com.hls.modules..*Controller.*(..))")
    public void pointcut(){ }

    @Before(value = "pointcut()")
    public void beforeLog(@NotNull JoinPoint point) throws JsonProcessingException {
        // 获取执行的方法名称
        String methodName = point.getSignature().getName();
        Object[] args = point.getArgs();
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        log.info("--"+ methodName +"-- >>> parameter：" + JSONChange.objToJson(args));
    }

    @AfterReturning(value = "pointcut()",returning = "retVal")
    public void afterReturning(@NotNull JoinPoint point, Object retVal) throws JsonProcessingException {
        // 获取执行的方法名称
        String methodName = point.getSignature().getName();
        log.info("--"+ methodName +"-- <<<    return：" + JSONChange.objToJson(retVal));
        log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
    }

    @AfterThrowing(value = "pointcut()" ,throwing = "throwable")
    public void afterThrow(Throwable throwable) {
        log.error(throwable.getMessage(), throwable);
        log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
    }

}
